<template>
  <div>
    <div class="goods">
      <div class="menu-wrapper" ref="menuWrapper">
        <ul>
          <li v-for="(item,index) in goods" class="menu-item" :class="{'current':currentIndex===index}"
              @click="selectMenu(index,$event)" ref="menuList">
	          <span class="text border-1px">
	            {{item.cateName}}
	          </span>
          </li>
        </ul>
      </div>
      <div class="foods-wrapper" ref="foodsWrapper">
        <ul>
          <li v-for="(item,index) in goods" class="food-list" ref="foodList" @click.stop="showlist()">
          	<h4 class="meunName" :class="{'meunNameFirst':index==0}">{{item.cateName}}</h4>
            <ul>
              <li @click="selectFood(food,$event)" v-for="food in item.foods" class="food-item border-1px">
                <div class="icon">
                  <img width="72" height="72" :src="food.picUrl">
                </div>
                <div class="content">
                  <h2 class="name">{{food.productName}}</h2>
                  <p class="desc">套餐搭配：</p>
                  <div class="extra">
                    <span class="count">{{food.productDetails}}</span>
                  </div>
                  <div class="price">
                    <span class="now"><span class="smallrmb">￥</span>{{food.productPrice}}</span>
                  </div>
                  <div class="cartcontrol-wrapper">
                  	<!--PromptShow && food.specNameList-->
                    <cartcontrol @add="addFood(food,arguments)" :food="food" :goodsMark="goodsMark"></cartcontrol>
                  </div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
    </div>
    <food @add="addFood" :food="selectedFood" v-on:listenFoodBack="hideFoodtoback" ref="food"></food>
    <ingredient ref="ingredient" v-on:listenFoodBack="hideFoodtoback"></ingredient>
    <transition name="fade">
      <div class="list-mask" @click="hideList" v-show="listShow"></div>
    </transition>
  </div>
</template>

<script type="text/ecmascript-6">
  import BScroll from 'better-scroll';
  import Vue from 'vue';
  import shopcart from 'components/shopcart/shopcart';
  import cartcontrol from 'components/cartcontrol/cartcontrol';
  import food from 'components/food/food';
  import ingredient from 'components/ingredient/ingredient';
  import Bus from 'common/js/bus.js';
  import {getProduct} from 'src/methods/request.js'

  export default {
    props: {

    },
    data() {
      return {
      	mchtId:"",
      	currentName:0,
        goods: [],//总食品列表
        listHeight: [],
        giveCartFood:[],//点击+号时获取的食品
        scrollY: 0,
        selectedFood: {},//已选择的食品列表
        listShow:false,
        foodNo:"",//视频NO
        goodsMark:"goodsMark",//goods组建的标记
        concatfoods:[],
        ingredArr:[]
      };
    },
    computed: {
      currentIndex() {
        for (let i = 0; i < this.listHeight.length; i++) {
          let height1 = this.listHeight[i];
          let height2 = this.listHeight[i + 1];
          if (!height2 || (this.scrollY >= height1 && this.scrollY < height2)) {
            this._followScroll(i);
            return i;
          }
        }
        return 0;
      },
      selectFoods() {
        let foods = [];
				for(let good of this.goods){
					for(let food of good.foods){
						 if (food.count) {
              foods.push(food);
            }
					}
				}
				foods = foods.concat(this.concatfoods);
       	return foods;
      }
    },
    created() {
    	//获取菜品信息
    	this.mchtId = sessionStorage.getItem("mchtId");
			getProduct({mchtId:10000094}).then(
						response => {
						this.goods = response.data;
						this.$nextTick(() => {
			        this._initScroll();
			        this._calculateHeight();
			      });
					}
				).catch(
					error => console.log(error)
				);
    },
    methods: {
    	//左侧商品类方法
      selectMenu(index, event) {
        if (!event._constructed) {
          return;
        }
        let foodList = this.$refs.foodList;
        let el = foodList[index];
        this.foodsScroll.scrollToElement(el, 300);
      },
      //右侧商品
      selectFood(food, event) {
        if (!event._constructed) {
          return;
        }
        this.selectedFood = food;
        this.$refs.food.show();
      },
      addFood(food,event,target) {
        //点击添加菜品时触发
      	if(parseInt(food.specStatus)){
      		this.showlist();
          this.$refs.ingredient.show();
          Bus.$emit('getCartFood', food);
      	};
        this._drop(target);
      },
      _drop(target,repeat) {
        // 体验优化,异步执行下落动画
          Bus.$emit('getTarget', target);
          Bus.$emit('getselectFoods', this.selectFoods);
      },
      showlist(){
      	//遮罩层显示
      	this.listShow = true
      },
      hideList(){
      	//遮罩层隐藏
      	this.listShow = false;
      	this.$refs.food.hide();
      	this.$refs.ingredient.hide();
      },
      hideFoodtoback(data){
      	//遮罩层隐藏
      	this.listShow = data;
      },
      _initScroll() {
      	//加载滚动事件
        this.meunScroll = new BScroll(this.$refs.menuWrapper, {
          click: true
        });
        this.foodsScroll = new BScroll(this.$refs.foodsWrapper, {
          click: true,
          probeType: 3
        });
        this.foodsScroll.on('scroll', (pos) => {
          // 判断滑动方向，避免下拉时分类高亮错误（如第一分类商品数量为1时，下拉使得第二分类高亮）
          if (pos.y <= 0) {
            this.scrollY = Math.abs(Math.round(pos.y));
          }
        });
      },
      _calculateHeight() {
        let foodList = this.$refs.foodList;
        let height = 0;
        this.listHeight.push(height);
        for (let i = 0; i < foodList.length; i++) {
          let item = foodList[i];
          height += item.clientHeight;
          this.listHeight.push(height);
        }
      },
      _followScroll(index) {
        let menuList = this.$refs.menuList;
        let el = menuList[index];
        this.meunScroll.scrollToElement(el, 300, 0, -100);
      }
    },
    components: {
      shopcart,
      cartcontrol,
      food,
      ingredient
    }
  };
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../common/stylus/mixin.styl"
  .content .name{
  	overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
		width: 110px;
  }
  .extra .count{
  	overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
		width: 110px;
		display: block;
  }
  .Prompt_information{
  	font-size: 12px;
  	position: absolute;
  	left: -200px;
  	top: 3px;
  	padding: 4px 8px;
  	background: rgba(0,0,0,.8);
  	color: #fff;
  	border-radius: 6px;
  }
  .meunName{
  	font-size: 12px;
  	color: #07111B;
  	padding: 12px 0 2px 12px;
  }
  .meunNameFirst{
  	padding: 0 0 2px 12px;
  }
	.icon img{
		border-radius: 4px;
	}
	.smallrmb{
		font-size: 9px;
	}
	.current{
		background: url(../../common/assets/01home_classify_s.png) no-repeat 10px center;
		background-size: 7px 7px;
	}
  .goods
    display: flex
    position: absolute
    top: 220px
    bottom: 48px
    width: 100%
    overflow: hidden
    .menu-wrapper
      flex: 0 0 88px
      background: #e8e8e8
      border-right:1px solid #d9d9d9
      padding-bottom:40px
      .menu-item
        display: table
        width: 56px
        padding: 15px 12px
        line-height: 14px
        &.current
          position: relative
          z-index: 10
          margin-top: -1px
          padding-left: 24px
          color:#e6893c
          font-weight: 700
          .text
            border-none()
        .icon
          display: inline-block
          vertical-align: top
          width: 12px
          height: 12px
          margin-right: 2px
          background-size: 12px 12px
          background-repeat: no-repeat
          &.decrease
            bg-image('decrease_3')
          &.discount
            bg-image('discount_3')
          &.guarantee
            bg-image('guarantee_3')
          &.invoice
            bg-image('invoice_3')
          &.special
            bg-image('special_3')
        .text
          width: 56px
          vertical-align: middle
          border-1px(rgba(7, 17, 27, 0.1))
          font-size: 12px
    .foods-wrapper
      flex: 1
      .title
        padding-left: 14px
        height: 26px
        line-height: 26px
        border-left: 2px solid #d9dde1
        font-size: 12px
        color: rgb(147, 153, 159)
        background: #f3f5f7
      .food-item
        display: flex
        padding: 12px 4px 12px 12px
        padding-bottom: 8px
        border-1px(rgba(7, 17, 27, 0.1))
        &:last-child
          border-none()
          margin-bottom: 0
        .icon
          flex: 0 0 72px
          margin-right: 10px
        .content
          flex: 1
          .name
            margin: 2px 0 4px 0
            height: 14px
            line-height: 14px
            font-size: 14px
            color: #323232
          .desc, .extra
            line-height: 12px
            font-size: 10px
            color: rgb(147, 153, 159)
          .desc
            line-height: 12px
            margin-bottom: 4px
          .extra
            .count
              margin-right: 12px
          .price
            font-weight: 700
            line-height: 24px
            .now
              margin-right: 8px
              font-size: 18px
              color: #e68a3f
            .old
              text-decoration: line-through
              font-size: 10px
              color: rgb(147, 153, 159)
          .cartcontrol-wrapper
            position: absolute
            right: 10px
            bottom: 11px
</style>
